﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_Menu_AddMenuD : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        txtTenChuDe.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; "); 
        if (!IsPostBack)
        {
            PrintDisplay();

            BindData();
        }
    }



    private void PrintDisplay()
    {
        if (Request.QueryString["UserID"] != "" && Request.QueryString["UserID"] != null)
        {
            lblDisplay.Text = "Người dùng: <b>" + Request.QueryString["UserNm"] + "</b>";

            lblDisplay1.Text = "Người dùng: <b>" + Request.QueryString["UserNm"] + "</b>";
            lblViewUser1.Visible = true;
            lblViewUser1.Text = "<a href='#' onclick='javascript:window.open(\"../User/ViewUser.aspx?UserID=" + Request.QueryString["UserID"] + "&CodeID=" + Request.QueryString["CodeID"] + "&AutID=" + Request.QueryString["AutID"] + "\",\"winRules\",\"status=1,menu=0,scrollbars=1\")'> Xem thông tin người dùng</a>";

        }
        else
        {
            lblViewUser.Visible = true;
            lblViewUser.Text = "<a href='#' onclick='javascript:window.open(\"../User/ViewUser.aspx?CodeID=" + Request.QueryString["CodeID"] + "&AutID=" + Request.QueryString["AutID"] + "\",\"winRules\",\"status=1,menu=0,scrollbars=1\")'> Xem danh sách người dùng</a>";
            lblDisplay.Text = "Quyền hạn: <b>" + Request.QueryString["AutNm"] + "</b>";

            lblViewUser1.Visible = true;
            lblViewUser1.Text = "<a href='#' onclick='javascript:window.open(\"../User/ViewUser.aspx?CodeID=" + Request.QueryString["CodeID"] + "&AutID=" + Request.QueryString["AutID"] + "\",\"winRules\",\"status=1,menu=0,scrollbars=1\")'> Xem danh sách người dùng</a>";
            lblDisplay1.Text = "Quyền hạn: <b>" + Request.QueryString["AutNm"] + "</b>";
        }
        lblGroupMn.Text = "<B>" + Request.QueryString["GroupName"] + "</B>";
    }
    private void BindData()
    {
        string menuid = "0";
        string Show_MK = CheckIsNull(Request.QueryString["Show_MK"]);
        if (Request.QueryString["groupid"] != "" && Request.QueryString["groupid"] != null)
        {
            btnSave.Visible = true;
            menuid = Request.QueryString["groupid"];
        }

        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("MenuName", txtTenChuDe.Text.Trim());
        msg.CreateFirstNode("LevelMenuID", menuid);
        msg.CreateFirstNode("KindNo", "A");

        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_MenuByCondition(msg.GetXmlStr);
        if (Request.QueryString["mtIndex"] == "0") //For User
        {
            MultiView1.ActiveViewIndex = 0;
            grv_Master.DataSource = ds;
            grv_Master.DataBind();
            BindStateCheckbox();
        }
        else //For Group
        {
            MultiView1.ActiveViewIndex = 1;
            grv_MasterGroup.DataSource = ds;
            grv_MasterGroup.DataBind();

        }
    }


    private void BindStateCheckbox()
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();

        string UserID = Request.QueryString["UserID"];
        string AutID = Request.QueryString["AutID"];
        string groupid = Request.QueryString["groupid"];
        msg.CreateFirstNode("MenuID", "0");
        msg.CreateFirstNode("UserID", CheckIsNull(UserID));
        msg.CreateFirstNode("AutID", CheckIsNull(AutID));
        DataTable dt = cls.Get_MenudByCondition(msg.GetXmlStr.ToString()).Tables[0];
        if (dt.Rows.Count > 0)
        {

            clsGetMenuAuth clsAut = new clsGetMenuAuth();
            clsAut.mn_DataTable = dt;
            clsAut.mn_AutID = AutID.ToString();
            clsAut.mn_UserID = UserID.ToString();
            clsAut.mn_MenuID = groupid;
            clsAut.mn_bCheckTable = true;

            if (clsAut.IsShowAuth())
            {
                divMaster.Enabled = true;
                chkAllShowMK.Checked = true;
            }
            else
            {
                divMaster.Enabled = false;
                chkAllShowMK.Checked = false;
               
                
            }



            for (int i = 0; i < grv_Master.Rows.Count; i++)
            {
                Label lblMenuID = (Label)grv_Master.Rows[i].Cells[0].FindControl("lblMenuID");
                CheckBox chkShowMK = new CheckBox();
                chkShowMK = (CheckBox)grv_Master.Rows[i].FindControl("chkShowMK");
                CheckBox chkAddMK = (CheckBox)grv_Master.Rows[i].FindControl("chkAddMK");
                CheckBox chkUpdMK = (CheckBox)grv_Master.Rows[i].FindControl("chkUpdMK");
                CheckBox chkDelMK = (CheckBox)grv_Master.Rows[i].FindControl("chkDelMK");
                CheckBox chkSignMK = (CheckBox)grv_Master.Rows[i].FindControl("chkSignMK");


                clsAut.mn_MenuID = lblMenuID.Text;
                //Show
                if (clsAut.IsShowAuth())
                {
                    chkShowMK.Checked = true;
                }
                else
                {
                    chkShowMK.Checked = false;
                }
                //Add
                if (clsAut.IsAddAuth())
                {
                    chkAddMK.Checked = true;
                }
                else
                {
                    chkAddMK.Checked = false;
                }
                //Upd
                if (clsAut.IsUpdateAuth())
                {
                    chkUpdMK.Checked = true;
                }
                else
                {
                    chkUpdMK.Checked = false;
                }
                //Del
                if (clsAut.IsDeleteAuth())
                {
                    chkDelMK.Checked = true;
                }
                else
                {
                    chkDelMK.Checked = false;
                }
                //Sign
                if (clsAut.IsSignAuth())
                {
                    chkSignMK.Checked = true;
                }
                else
                {
                    chkSignMK.Checked = false;
                }


            }
        }
        else
        {

            divMaster.Enabled=false;
            chkAllShowMK.Checked = false;
          


        }


    }
    protected void grv_Master_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.className='cursorOverGrid'");
            if (e.Row.RowIndex % 2 == 0)
                e.Row.Attributes.Add("onmouseout", "this.className='gvRowStyle'");
            else
                e.Row.Attributes.Add("onmouseout", "this.className='gvAlternatingRowStyle'");

            //   Label lblMenuID = (Label)e.Row.FindControl("lblMenuID");

            //    string UserID = Request.QueryString["UserID"];
            //    string AutID = Request.QueryString["AutID"];

            //    clsWebShop cls = new clsWebShop();
            //    PccMsg msg=new PccMsg();
            //    msg.CreateFirstNode("MenuID", CheckIsNull(lblMenuID.Text));
            //    msg.CreateFirstNode("UserID", CheckIsNull(UserID));
            //    msg.CreateFirstNode("AutID", CheckIsNull(AutID));
            //    DataTable dt = cls.Get_MenudByCondition(msg.GetXmlStr.ToString()).Tables[0];
            //    if (dt.Rows.Count > 0)
            //    {
            //        CheckBox chkShowMK = (CheckBox)e.Row.FindControl("chkShowMK");
            //                  /* CheckBox chkAddMK = (CheckBox)e.Row.FindControl("chkAddMK");
            //        CheckBox chkUpdMK = (CheckBox)e.Row.FindControl("chkUpdMK");
            //        CheckBox chkDelMK = (CheckBox)e.Row.FindControl("chkDelMK");*/
            //        CheckState(dt.Rows[0]["show_mk"].ToString(), chkShowMK);
            //        /*CheckState(dt.Rows[0]["add_mk"].ToString(), chkAddMK);
            //        CheckState(dt.Rows[0]["upd_mk"].ToString(), chkUpdMK);
            //        CheckState(dt.Rows[0]["del_mk"].ToString(), chkDelMK);*/


            //    }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }
    protected void btnSearch_Click1(object sender, EventArgs e)
    {
        BindData();
    }
    protected void ddlChuDe_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["groupid"] != "" && Request.QueryString["groupid"] != null)
            Response.Redirect("AddMenuD.aspx?CodeID=" + Request.QueryString["CodeID"] + "&GroupName=" + Request.QueryString["GroupName"] + "&AutNm=" + Request.QueryString["AutNm"] + "&UserNm=" + Request.QueryString["UserNm"] + "&AutID=" + Request.QueryString["AutID"] + "&UserID=" + Request.QueryString["UserID"] + "&index=" + Request.QueryString["index"]);
        else
            Response.Redirect("MenuD.aspx?CodeID=" + Request.QueryString["CodeID"] + "&mtIndex=" + Request.QueryString["mtIndex"] + "&index=" + Request.QueryString["index"]);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        string UserID = CheckIsNull(Request.QueryString["UserID"]);
        string AutID = CheckIsNull(Request.QueryString["AutID"]);
        string MenuID = CheckIsNull(Request.QueryString["groupid"]);
        string Show_MKGroup = StateCheck(chkAllShowMK);


        if (UserID != "0") //Cap nhat cho User
        {
            if (MenuID != "0") //Cap nhat GroupMenu
            {

                PccMsg msg = new PccMsg();
                msg.CreateFirstNode("MenuID", MenuID);
                msg.CreateFirstNode("UserID", UserID);
                msg.CreateFirstNode("AutID", AutID);
                msg.CreateFirstNode("show_mk", Show_MKGroup);
                msg.CreateFirstNode("add_mk", "N");
                msg.CreateFirstNode("upd_mk", "N");
                msg.CreateFirstNode("del_mk", "N");
                msg.CreateFirstNode("rpt_mk", "N");
                msg.CreateFirstNode("send_mk", "N");
                cls.Ins_Upd_Menud(msg.GetXmlStr.ToString());


            }

            for (int i = 0; i < grv_Master.Rows.Count; i++)
            {
                Label lblMenuID = (Label)grv_Master.Rows[i].Cells[0].FindControl("lblMenuID");
                CheckBox chkShowMK = (CheckBox)grv_Master.Rows[i].FindControl("chkShowMK");
                CheckBox chkAddMK = (CheckBox)grv_Master.Rows[i].FindControl("chkAddMK");
                CheckBox chkUpdMK = (CheckBox)grv_Master.Rows[i].FindControl("chkUpdMK");
                CheckBox chkDelMK = (CheckBox)grv_Master.Rows[i].FindControl("chkDelMK");
                CheckBox chkSignMK = (CheckBox)grv_Master.Rows[i].FindControl("chkSignMK");


                string ShowMK = StateCheck(chkShowMK);
                string AddMK = StateCheck(chkAddMK);
                string UpdMK = StateCheck(chkUpdMK);
                string DelMK = StateCheck(chkDelMK);
                string SignMK = StateCheck(chkSignMK);

                PccMsg msg = new PccMsg();
                msg.CreateFirstNode("MenuID", lblMenuID.Text);
                msg.CreateFirstNode("UserID", UserID);
                msg.CreateFirstNode("AutID", AutID);
                msg.CreateFirstNode("show_mk", ShowMK);

                msg.CreateFirstNode("add_mk", AddMK);
                msg.CreateFirstNode("upd_mk", UpdMK);
                msg.CreateFirstNode("del_mk", DelMK);
                msg.CreateFirstNode("sign_mk", SignMK);
                msg.CreateFirstNode("rpt_mk", "N");
                msg.CreateFirstNode("send_mk", "N");
                cls.Ins_Upd_Menud(msg.GetXmlStr.ToString());
            }
        }
        else //Group
        {
            DataTable dt = new DataTable();
            dt = cls.Get_UserByAutIDNoneProc(AutID).Tables[0];
            if (dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    UserID = dt.Rows[j]["UserID"].ToString();
                    /*if (MenuID != "0")
                    {

                        PccMsg msg = new PccMsg();
                        msg.CreateFirstNode("MenuID", MenuID);
                        msg.CreateFirstNode("UserID", UserID);
                        msg.CreateFirstNode("AutID", AutID);
                        msg.CreateFirstNode("show_mk", Show_MK);
                        msg.CreateFirstNode("add_mk", "N");
                        msg.CreateFirstNode("upd_mk", "N");
                        msg.CreateFirstNode("del_mk", "N");
                        msg.CreateFirstNode("rpt_mk", "N");
                        msg.CreateFirstNode("send_mk", "N");
                        cls.Ins_Upd_Menud(msg.GetXmlStr.ToString());


                    }*/

                    for (int i = 0; i < grv_Master.Rows.Count; i++)
                    {
                        Label lblMenuID = (Label)grv_Master.Rows[i].Cells[0].FindControl("lblMenuID");
                        CheckBox chkShowMK = (CheckBox)grv_Master.Rows[i].FindControl("chkShowMK");
                        CheckBox chkAddMK = (CheckBox)grv_Master.Rows[i].FindControl("chkAddMK");
                        CheckBox chkUpdMK = (CheckBox)grv_Master.Rows[i].FindControl("chkUpdMK");
                        CheckBox chkDelMK = (CheckBox)grv_Master.Rows[i].FindControl("chkDelMK");
                        CheckBox chkSignMK = (CheckBox)grv_Master.Rows[i].FindControl("chkSignMK");


                        string ShowMK = StateCheck(chkShowMK);
                        string AddMK = StateCheck(chkAddMK);
                        string UpdMK = StateCheck(chkUpdMK);
                        string DelMK = StateCheck(chkDelMK);
                        string SignMK = StateCheck(chkSignMK);

                        PccMsg msg = new PccMsg();
                        msg.CreateFirstNode("MenuID", lblMenuID.Text);
                        msg.CreateFirstNode("UserID", UserID);
                        msg.CreateFirstNode("AutID", AutID);
                        msg.CreateFirstNode("show_mk", ShowMK);

                        msg.CreateFirstNode("add_mk", AddMK);
                        msg.CreateFirstNode("upd_mk", UpdMK);
                        msg.CreateFirstNode("del_mk", DelMK);
                        msg.CreateFirstNode("sign_mk", SignMK);
                        msg.CreateFirstNode("rpt_mk", "N");
                        msg.CreateFirstNode("send_mk", "N");
                        cls.Ins_Upd_Menud(msg.GetXmlStr.ToString());
                    }

                }
            }
        }

        /*else
        {
            if (MenuID != "0")
            {

                PccMsg msg = new PccMsg();
                msg.CreateFirstNode("MenuID", MenuID);
                msg.CreateFirstNode("UserID", UserID);
                msg.CreateFirstNode("AutID", AutID);
                msg.CreateFirstNode("show_mk", Show_MK);
                msg.CreateFirstNode("add_mk", "N");
                msg.CreateFirstNode("upd_mk", "N");
                msg.CreateFirstNode("del_mk", "N");
                msg.CreateFirstNode("rpt_mk", "N");
                msg.CreateFirstNode("send_mk", "N");
                cls.Ins_Upd_Menud(msg.GetXmlStr.ToString());


            }
        }*/
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Bảo lưu thành công')</script>");

        BindData();
    }

    private string StateCheck(CheckBox chk)
    {
        string state = "N";
        if (chk.Checked == true)
            state = "Y";
        return state;
    }
    private void CheckState(string state, CheckBox chk)
    {

        if (state == "Y")
            chk.Checked = true;
        else
            chk.Checked = false;
    }
    private string CheckIsNull(string value)
    {
        string sReturn = "0";
        if (value != "" && value != null) sReturn = value;
        return sReturn;
    }
    protected void grv_MasterGroup_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblMndName = (Label)e.Row.FindControl("lblMndName");
            lblMndName.Text = "<a href='AddMenuD.aspx?CodeID=" + Request.QueryString["CodeID"] + "&GroupName=" + DataBinder.Eval(e.Row.DataItem, "MenuName").ToString() + "&AutNm=" + Request.QueryString["AutNm"] + "&UserNm=" + Request.QueryString["UserNm"] + "&AutID=" + Request.QueryString["AutID"] + "&UserID=" + Request.QueryString["UserID"] + "&index=" + Request.QueryString["index"] + "&mtIndex=0&groupid=" + DataBinder.Eval(e.Row.DataItem, "MenuID").ToString() + "' style='color:Green;text-decoration: none'   onmouseover=\"this.style.textDecoration='underline';  this.style.fontWeight='bold'\"  onmouseout=\"this.style.textDecoration='none'; this.style.fontWeight='normal'\">" + DataBinder.Eval(e.Row.DataItem, "MenuName").ToString() + "</a>";
        }
    }
}
